package com.fmdrama;

import java.util.List;

import com.fmdrama.bean.Profile;
import com.fmdrama.bean.Video;
import com.fmdrama.db.ProfileDao;
import com.fmdrama.db.VideoDao;
import com.fmdrama.fetcher.HomeFetcher;
import com.fmdrama.fetcher.ListFetcher;
import com.fmdrama.fetcher.PlayFetcher;
import com.fmdrama.util.Configuration;
import com.fmdrama.util.LOG;

/**
 * 爬行者，主要负责调度抓取器和持久化数据
 * @author Administrator
 */
public class CategoryCrawler {

	private ListFetcher listFetcher = new ListFetcher();
	private PlayFetcher playFetcher = new PlayFetcher();
	private ProfileDao profileDao = new ProfileDao();
	private VideoDao videoDao = new VideoDao();
	private int category = 0;
	
	public CategoryCrawler(int category){
		this.category = category;
	}
	
	/**
	 * 开始抓取
	 */
	public void start(){
		Main.self.cateStatus.reset();
		new Thread(){
			public void run() {
				crawl();
				Main.self.stopCate();
			};
		}.start();
	}
	
	/**
	 * 爬行，先抓取首页数据，然后进入每个播放页面抓取数据
	 */
	private void crawl(){
		List<Profile> profiles = listFetcher.fetch(category); //抓取首页
		if(null != profiles){
			Main.self.cateStatus.setTotal(profiles.size());
			for(int i=0; i<profiles.size()&&Main.self.cateStarted; i++){ //循环列表页面抓取到的数据，进入每个播放页面抓取数据
				Profile profile = profiles.get(i);
				try{
					profileDao.insertOrUpdate(profile); //持久化列表页面数据
				}catch (Exception e) {
					e.printStackTrace();
				}
				List<Video> videos = playFetcher.fetch(profile); //抓取播放页面数据
				try{
					if(null != videos && videos.size() > 0){
						videoDao.insert(videos); //持久化播放页面数据
						LOG.info(profile.toString() + "， 抓取：" + videos.size());
					}
				}catch (Exception e) {
					e.printStackTrace();
				}
				Main.self.cateStatus.setComplete(i+1); //更新进度
			}
		}
		Main.self.cateStatus.loopCountIncr();
	}
	
	/**
	 * 停止抓取
	 */
	public void stop(){
		listFetcher.close();
		playFetcher.close();
	}
	
}
